package com.heiyu.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.heiyu.api.request.PushInfoRequest;
import com.heiyu.api.response.BaseInitResponse;
import com.heiyu.api.response.CommonResponse;
import com.heiyu.api.response.model.BaseInitModel;
import com.heiyu.controller.ApiBaseController;
import com.heiyu.dao.ChannelInfoMapper;
import com.heiyu.dao.ClientPushInfoMapper;
import com.heiyu.entity.ChannelInfo;
import com.heiyu.entity.ClientPushInfo;
import com.heiyu.utils.CacheKey;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api
@RestController
@RequestMapping("api/v1/base")
public class VersionController extends ApiBaseController {


    @Autowired
    private ClientPushInfoMapper clientPushInfoMapper;
    @Autowired
    private ChannelInfoMapper channelInfoMapper;

    @ApiOperation(value = "init", notes = "初始化")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "channel",value = "渠道信息"),
            @ApiImplicitParam(name = "version",value = "版本号")})
    @RequestMapping(value = "init", method = RequestMethod.GET)
    public BaseInitResponse init(@RequestParam("channel")String channel,@RequestParam("version")String version) {
        BaseInitResponse response = new BaseInitResponse();
        response.success();
        BaseInitModel model = JSON.parseObject(redisUtils.get(CacheKey.BASE_INIT_MODEL),BaseInitModel.class);
        model.setAboutUsLogoUrl(model.getWebSiteCode());
        JSONArray array =  new JSONArray();
        for(int i=0;i<1;i++){
            JSONObject obj = new JSONObject();
            obj.put("icon",model.getAboutUsLogoUrl());
            obj.put("desc","新的版本有更好的界面");
            array.add(obj);
        }
        model.setAndroidMustUpdate("0");
        model.setAndroidVersion("7");
        model.setIosMustUpdate("0");
        model.setIosVersion("3.1");
        model.setUpdateDesc(array.toJSONString());
        model.setShowHeiyu("1");
        if(channel.equals("vivo")||
                channel.equals("lenovo")||
              channel.equals("youyi"))
        model.setShowTab("0");
        model.setCoinJsonUrl("https://dafenqi.oss-cn-hangzhou.aliyuncs.com/1531209673626637.json");
        response.setData(model);
        return response;
    }


    @ApiOperation(value = "push/put", notes = "上传推送客户端数据")
    @RequestMapping(value = "push/put", method = RequestMethod.POST)
    public CommonResponse uploadPushId(@RequestBody PushInfoRequest request){
        CommonResponse response = new CommonResponse();
        if(StringUtils.isEmpty(request.getClientType())||StringUtils.isEmpty(request.getClientVersion())||StringUtils.isEmpty(request.getPushId())){
            response.error("参数不正确");
        }else{
            ClientPushInfo info = new ClientPushInfo();
            info.setPushId(request.getPushId());
            List<ClientPushInfo> list = this.clientPushInfoMapper.selectList(info);
            if(CollectionUtils.isEmpty(list)){
                info.setClientType(request.getClientType());
                info.setClientVersion(request.getClientVersion());
                info.setIsDeleted("0");
                info.setCreatedTime(new Date());
                info.setUpdateTime(new Date());
                this.clientPushInfoMapper.insert(info);
            }
            if(!StringUtils.isEmpty(request.getIdfa())){
                ChannelInfo channelInfo = new ChannelInfo();
                channelInfo.setAppId(request.getAppId());
                channelInfo.setIdfa(request.getIdfa());
                channelInfo.setCreatedTime(new Date());
                channelInfo.setModifyTime(new Date());
                channelInfo.setIsDeleted("0");
                this.channelInfoMapper.insert(channelInfo);
            }
            response.success();
        }
        return response;
    }

}
